package lanbo.cj.lmp.activity;

import cn.bmob.v3.BmobUser;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UpdateListener;
import lanbo.cj.lmp.R;
import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
/**更改密码界面
 * 
 * */
public class ChangePswActivity extends Activity {
	private ImageView mBack;
	private EditText oldPswEd,newPswEd,surePswEd;
	private Button mSure;
	private BmobUser currentUser;
	private String oldPsw,newPsw,surePsw;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.changepsw);
		findbyid();
		setListener();
		currentUser= BmobUser.getCurrentUser(ChangePswActivity.this);
	};

	private void findbyid(){
		mBack = (ImageView)findViewById(R.id.change_back);
		oldPswEd = (EditText)findViewById(R.id.et_oldpsw);
		newPswEd = (EditText)findViewById(R.id.et_newpsw);
		surePswEd = (EditText)findViewById(R.id.et_surepsw);
		mSure = (Button) findViewById(R.id.change_btn);
	}
	
	private void setListener(){
		mBack.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		
		mSure.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				resetPsw();
			}
		});
	}

	private void resetPsw() {
		// TODO Auto-generated method stub
		oldPsw = oldPswEd.getText().toString();
		newPsw = newPswEd.getText().toString();
		surePsw = surePswEd.getText().toString();
		if (TextUtils.isEmpty(oldPsw) || TextUtils.isEmpty(newPsw)) {
			Toast.makeText(ChangePswActivity.this, "密码不能为空", Toast.LENGTH_SHORT).show();
			return;
		}
		//判断确定密码和新密码是否一致
		if (!newPsw.equals(surePsw)) {
			Toast.makeText(ChangePswActivity.this, "确认密码不正确", Toast.LENGTH_SHORT).show();
			return;
		}
		
		currentUser.setPassword(oldPsw);
		currentUser.login(ChangePswActivity.this, new SaveListener() {
			
			@Override
			public void onSuccess() {
				// TODO Auto-generated method stub
				currentUser.setPassword(newPsw);
				currentUser.update(ChangePswActivity.this,new UpdateListener() {
					
					@Override
					public void onSuccess() {
						// TODO Auto-generated method stub
						Toast.makeText(ChangePswActivity.this,"密码修改成功", Toast.LENGTH_SHORT).show();
					}
					
					@Override
					public void onFailure(int arg0, String arg1) {
						// TODO Auto-generated method stub
						Toast.makeText(ChangePswActivity.this,"密码修改失败"+arg1, Toast.LENGTH_SHORT).show();
					}
				});
			}
			
			@Override
			public void onFailure(int arg0, String arg1) {
				// TODO Auto-generated method stub
				Toast.makeText(ChangePswActivity.this, "旧密码不正确", Toast.LENGTH_SHORT).show();
				System.out.print(arg1);
				return;
			}
		});
		
		
		
	}
}
